#property copyright "Copyright 2012"
#property link      "mingxinhe@gmail.com"

#import "mt4emailbridge.dll"
   int SendEmail(string SMTPServer, int SMTPPort, string SMTPUsername, string SMTPPassword, int RequiresSSL, string EmailFromAddress, string EmailFromName, string EmailToAddress, string EmailToName, string CCRecipient, string BCCRecipient, string Subject, string Body, int IsHtmlEmail, string AttachedFiles, int Flags);
#import

#property indicator_chart_window
#property indicator_buffers 4
#property  indicator_color1  Red
#property  indicator_color2  Aqua
#property  indicator_color3  Red
#property  indicator_width3  2
#property  indicator_color4  DodgerBlue
#property  indicator_width4  2

extern double MA.Cross.Buffer = 1;
extern int MA.Fast.Period=13;
extern int MA.Fast.Shift=0;
extern int MA.Slow.Period=20;
extern int MA.Slow.Shift=0;
extern string MA.Applied.Price.Desc = "0:PRICE_CLOSE;1:PRICE_OPEN;2:PRICE_HIGH;3:PRICE_LOW";
extern int MA.Applied.Price = 0;
extern string MA.Method.Desc = "0:MODE_SMA;1:MODE_EMA;2:MODE_SMMA;3:MODE_LWMA";
extern int MA.Method=0;
extern color Down.Arrow.Color=Red;
extern color Up.Arrow.Color=Aqua;
extern color MA.Fast.Color=Red;
extern color MA.Slow.Color=DodgerBlue;
extern bool Text.Alert = true;
extern bool Sound.Alert = true;
extern bool Email.Alert = true;
extern string SoundFileName = "Alert.wav";
extern string mobileNumber = "0413106070";
//---- buffers
double upArrow[];
double downArrow[];
//---- buffers
double EMAFastBuffer[];
double EMASlowBuffer[];

int digitFactor = 1;
datetime lastTime, lastAlertTime,firstTime;
bool upArrowSignal = false;
bool downArrowSignal = false;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
  //---- 6 additional buffers are used for counting.   
   if(Bid<10 && Digits==5) { digitFactor=10;}
   if(Bid<10 && Digits==4) { digitFactor= 1;}

   if(Bid>10 && Digits==3) { digitFactor=10;}
   if(Bid>10 && Digits==2) { digitFactor= 1;} 
   
//---- drawing settings 
   SetIndexStyle(0,DRAW_ARROW, EMPTY, 3, Down.Arrow.Color);
   SetIndexArrow(0,234);
   SetIndexBuffer(0, downArrow);
      
   SetIndexStyle(1,DRAW_ARROW, EMPTY, 3, Up.Arrow.Color);
   SetIndexArrow(1,233);
   SetIndexBuffer(1, upArrow);      
      
   SetIndexBuffer(2,EMAFastBuffer);
   SetIndexStyle(2, DRAW_LINE, STYLE_SOLID, 3, MA.Fast.Color);
   
   SetIndexBuffer(3,EMASlowBuffer);  
   SetIndexStyle(3,DRAW_LINE, STYLE_SOLID, 3, MA.Slow.Color);
   
      
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
   
   MA.Cross.Buffer *= digitFactor;
   
   
//---- initialization done
   return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+

int deinit()
{
   return(0);
}

//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence                           |
//+------------------------------------------------------------------+
int start()
{  
   int result = sendEmail("MACross LONG Alert" , "tttttt", mobileNumber);
   Print("result:",result); 
   
   double AvgRange;

   if(Bars<=MA.Slow.Period) return(0);
        
   int  counted_bars=IndicatorCounted();        
   if(counted_bars>0) 
   {
       int limit=Bars-counted_bars;
   }
   else
   {
       limit = Bars-MA.Slow.Period+1;
   }        
   
   while(limit >= 0)
   {  
       EMAFastBuffer[limit]=iMA(NULL,0,MA.Fast.Period,MA.Fast.Shift,MA.Method,MA.Applied.Price,limit);     
       EMASlowBuffer[limit]=iMA(NULL,0,MA.Slow.Period,MA.Slow.Shift,MA.Method,MA.Applied.Price,limit);  
      
       if (limit == 0) 
       {      
          if(!isNewBar()) break;
       }  

        upArrow[limit+1] = 0;
        downArrow[limit+1] = 0; 
  
        AvgRange = 0;              
        for (int counter=limit+1 ;counter<=limit+10;counter++)
        {
            AvgRange=AvgRange+MathAbs(High[counter]-Low[counter]);
        }
        double Range=AvgRange/10;
   
        if (EMAFastBuffer[limit+1] < EMASlowBuffer[limit+1] - MA.Cross.Buffer*Point && EMAFastBuffer[limit + 2] >= EMASlowBuffer[limit + 2] - MA.Cross.Buffer*Point)
        {                                                                              
             downArrow[limit+1] = EMASlowBuffer[limit+1] + Range*0.4;
             string alertStr = "Pair:" + Symbol() + ", T/F:" +  getTimeFrame(Period()) + ", Direction: SHORT, Price:" + DoubleToStr(Bid, Digits);                   
             if (Text.Alert && limit == 0)
             {
                 Alert(alertStr);
                 Print(alertStr);
             }
             if (Email.Alert && limit == 0)
             {
                 sendEmail("MACross SHORT Alert" , alertStr, mobileNumber);  
             }
             if (Sound.Alert && limit == 0)
             {
                 PlaySound(SoundFileName);
                 lastAlertTime = TimeCurrent(); 
             }                                                                                     
       }
       
       if (EMAFastBuffer[limit+1] > EMASlowBuffer[limit+1] + MA.Cross.Buffer*Point && EMAFastBuffer[limit + 2] <= EMASlowBuffer[limit + 2] + MA.Cross.Buffer*Point)
       {                                                                              
             upArrow[limit+1] = EMASlowBuffer[limit+1] - Range*0.4;
             alertStr = "Pair:" + Symbol() + ", T/F:" +  getTimeFrame(Period()) + ", Direction: LONG, Price:" + DoubleToStr(Ask, Digits);                   
             if (Text.Alert && limit == 0)
             {
                 Alert(alertStr);
                 Print(alertStr);
             }
             if (Email.Alert && limit == 0)
             {
                 sendEmail("MACross LONG Alert" , alertStr, mobileNumber);  
             }
             if (Sound.Alert && limit == 0)
             {
                 PlaySound(SoundFileName);
                 lastAlertTime = TimeCurrent(); 
             }                                                                                     
       }
      
       limit--;
   } 
//---- done
   return(0);  
}

int sendEmail(string subject, string body, string pMobileNumber)
{
    int rst = SendEmail("smtp.gmail.com", 587, "mingxinhe@gmail.com", "Pass3%word1234",
    1, "mingxinhe@gmail.com", "Len", "4982913@qq.com", "Len", "", "",
    subject, body, 0, 
    "", 1);
    return (rst);
}
  
bool isNewBar()
{    
    if (lastTime == Time[0]) 
    {        
        return (false);           
    }
    lastTime = Time[0];
    return (true); 
}

string getTimeFrame(int _Period)
{
    switch (_Period)
    {
        case 1: return ("M1");
                break;
        case 5: return ("M5");
                break;
        case 15: return ("M15");
                break;
        case 30: return ("M30");
                break;
        case 60: return ("H1");
                break;
        case 240: return ("H4");
                break;
        case 1440: return ("D1");
                break;
        case 10080: return ("W1");
                break;
        case 43200: return ("Month");
                break;
    }
}
 